package com.diors.game.engine.arpg.scene;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;

import com.diors.game.engine.arpg.object.AbstractObject;
import com.diors.game.engine.arpg.object.PlayerBase;
import com.diors.game.engine.arpg.scene.SceneFactory.SceneAdapter;
import com.diors.game.engine.arpg.scene.entity.MapEntity;
import com.diors.game.engine.arpg.scene.entity.SceneEntity;
import com.diors.game.engine.arpg.scene.entity.SceneObject;
import com.diors.game.engine.arpg.scene.entity.ScreenEntity;

/**
 * 场景引擎
 * @author	darren.ouyang <ouyang.darren@gmail.com>
 * @date	2013-10-22
 * @version 1.0
 * @copyright Copyright (c) 2013, darren.ouyang
 */
public class SceneEngine 
{
	
	final static Logger log = Logger.getLogger(SceneEngine.class);  
	
	final static SceneManager sceneManager = new SceneManager();
	
	/**
	 * 创建场景
	 * @param model	场景Model
	 * @return		场景实体
	 * @create	2013-10-22	darren.ouyang
	 */
	public static SceneEntity createScene (SceneAdapter adapter)
	{
		SceneEntity scene = SceneFactory.createScene(adapter);
		if (scene == null)
			return null;
		
		/* 加载Npc */
		/*for (MapEntity map : scene.getLines())
			NpcEngine.loadNpcs(scene, map, adapter.bornList);
		
		
		 注册场景 
		sceneManager.register(scene);
		log.info(new StringBuilder("创建场景成功--场景ID[").append(scene.getID()).
				append("]原型ID[").append(scene.getBaseID()).append("]场景类型[").
				append(scene.gainSceneType()).append("]线数[").append(scene.getLines().size()).
				append("]摧毁时间[").append(scene.getDestroyTime()).append("]").toString());*/
		return scene;
	}
	
	/**
	 * 进入场景
	 * @param player	角色
	 * @param map		地图
	 * @param x			目标点X
	 * @param y			目标点Y
	 * @author	darren.ouyang
	 * @create	2013年11月11日 上午10:43:12
	 */
	public static void enterScene (PlayerBase player, MapEntity map, int x, int y)
	{
		SceneObject sceneObject = player.getSceneObject();
		sceneObject.setMap(map);

		ScreenEntity screen = getScreen(player);
		map.getPlayers().put(player.getId(), player);
		screen.getPlayers().put(player.getId(), player);
		
		//Collection<PlayerBase> players = getScreenPlayer(player);
	}
	
	/**
	 * 退出场景
	 * @param player	玩家实体
	 * @author	darren.ouyang
	 * @create	2013年11月11日 下午6:20:10
	 */
	public static void exitScene (PlayerBase player)
	{
		SceneObject sceneObject = player.getSceneObject();
		MapEntity map = sceneObject.getMap();
		
		ScreenEntity screen = getScreen(player);
		map.getNpcs().remove(player.getId());
		screen.getNpcs().remove(player.getId());
		
		//Collection<PlayerBase> players = getScreenPlayer(player);
	}
	
	public static void move (AbstractObject object, int x, int y, int targetX, int targetY)
	{
		/*SceneObject sceneObject = object.getSceneObject();
		long time = System.currentTimeMillis();
		int dirX = targetX - x;
		int dirY = targetY - y;*/
		
		//AttrObject attrObject = object.getAttr();
		//float s = attrObject.getValueFloat(AttrEnum.MV);
		
		
	}
	
	public static void stopMove (AbstractObject object, int x, int y)
	{
		//SceneObject sceneObject = object.getSceneObject();
		
	}
	
	//private static void 
	
	/**
	 * 获取屏实体
	 * @param object
	 * @return
	 * @author	darren.ouyang
	 * @create	2013年11月11日 下午6:18:27
	 */
	private static ScreenEntity getScreen (AbstractObject object)
	{
		SceneObject sceneObject = object.getSceneObject();
		int screenId = 0;// ScreenUtils.getInstance().getScreenID(sceneObject.getX(), sceneObject.getY());
		ScreenEntity screen = sceneObject.getMap().getScreans().get(screenId);
		return screen;
	}
	
	/**
	 * 获取指定九宫格的所有玩家
	 * @param object	指定对象
	 * @return
	 * @author	darren.ouyang
	 * @create	2013年11月11日 下午3:04:34
	 */
	public static Collection<PlayerBase> getScreenPlayer (AbstractObject object)
	{
		List<PlayerBase> list = new ArrayList<PlayerBase>();
		
		//SceneObject sceneObject = object.getSceneObject();
		//MapEntity map = sceneObject.getMap();
		//SceneEntity scene = map.getScene();
		//int x = sceneObject.getX();
		//int y = sceneObject.getY();
		//int maxX = 0;// scene.getMaxX();
		//int maxY = 0;//scene.getMaxY();
		/*List<Integer> screenIdList = ScreenUtils.getInstance().getSudokuScreenIDs(maxX, maxY, x, y);
		for (int screenId : screenIdList)
		{
			ScreenEntity screen = map.getScreans().get(screenId);
			list.addAll(screen.getPlayers().values());
		}*/
		
		return list;
	}

}
